﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace UpLoad
{
    public class ImageUpLoad : UpLoadBase
    {
        
        
        /// <summary>
        /// 重写父类的上传图片方法
        /// </summary>
        /// <param name="uploadfile"></param>
        /// <returns></returns>
        public override ReturnModel SaveFile(System.Web.UI.WebControls.FileUpload uploadfile)
        {
            ReturnModel model = new ReturnModel();
            string suffix = GetSuffix(uploadfile.FileName);         //获取扩展名
            string filename = GetOnlyName(suffix);                  //获取生成的文件名
            string uploadPath = Config.ImageUploadPath.Trim() == "" ? Config.DefaultPath : Config.ImageUploadPath;      //获取上传路径
            string fullname = Path.Combine(uploadPath,filename );          //获取完整的上传路径和文件名
            if (!Config.imagesuffixs.Contains(suffix))        //判断是否为上传类型
            {
                model.IsSuccess = false;
            }
            else if (UpLoad(uploadfile, fullname))              //判断是否上传成功
            {
                FileInfoModel info = new FileInfoModel();
                info.filename = filename;
                info.filetype = GetFileType(suffix);
                info.createtime = DateTime.Now;
                info.updatetime = DateTime.Now;
                model.FileInfo = info;
                if(new FileDal().AddFile(model.FileInfo))
                    model.IsSuccess = true;
            }
            else
            {
                model.IsSuccess = false;
            }          
            return model;
        }

        //保存有缩略图的图片
        public ReturnModel SaveFile(System.Web.UI.WebControls.FileUpload uploadfile,ZoomType zoomtype)
        {
            ReturnModel model = new ReturnModel();
            string suffix = GetSuffix(uploadfile.FileName);         //获取扩展名
            string filename = GetOnlyName(suffix);                  //获取生成的文件名
            string uploadPath = Config.ImageUploadPath.Trim() == "" ? Config.DefaultPath : Config.ImageUploadPath;      //获取上传路径
            string fullname = Path.Combine(uploadPath,filename );          //获取完整的上传路径和文件名
            if (!Config.imagesuffixs.Contains(suffix))        //判断是否为上传类型
            {
                model.IsSuccess = false;
            }
            else if (UpLoad(uploadfile, fullname))              //判断是否上传成功
            {
                FileInfoModel info = new FileInfoModel();
                info.filename = filename;
                info.filetype = GetFileType(suffix);
                info.createtime = DateTime.Now;
                info.updatetime = DateTime.Now;                
                if (zoomtype == ZoomType.Size)
                    info.filenameA = GetSmallImageBySize(fullname, suffix, uploadPath);
                if (zoomtype == ZoomType.Multiple)
                    info.filenameA = GetSmallImageByMultiple(fullname, suffix, uploadPath);
                model.FileInfo = info;
                if (new FileDal().AddFile(model.FileInfo))
                    model.IsSuccess = true;
            }
            else
            {
                model.IsSuccess = false;
            }          
            return model;
        }

        //获取显示图片的路径
        public override string GetServerPath(string filename)
        {
            string filepath = Config.ImageServerPath.Trim() == "" ? Config.DefaultServerPath : Config.ImageServerPath;
            return Path.Combine(filepath, filename).ToString();
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public override bool DeleteRecord(string filename)
        {
            string filepath = Config.ImageServerPath.Trim() == "" ? Config.DefaultServerPath : Config.ImageServerPath;
            return RemoveFile(filepath, filename);
        }

        //删除有缩略图的图片
        public bool DeleteRecord(string filename, string filenameA)
        {
            string filepath = Config.ImageServerPath.Trim() == "" ? Config.DefaultServerPath : Config.ImageServerPath;
            if (RemoveFile(filepath, filename) && RemoveFile(filepath, filenameA))
                return true;
            return false;
        }

        //根据缩略图的尺寸来截取上传缩略图并获取的名称
        public string GetSmallImageBySize(string fullname,string suffix,string path)
        {
            int width = Config.thumbnailWidth;
            int height = Config.thumbnailHeight;
            System.Drawing.Image image,NewImage;
            image = System.Drawing.Image.FromFile(fullname);
            System.Drawing.Image.GetThumbnailImageAbort callb =new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);    
            NewImage=image.GetThumbnailImage(width,height,callb,new System.IntPtr());
            string newname = GetOnlyName(suffix);
            string newfullname = Path.Combine(path, GetOnlyName(suffix));
            NewImage.Save(newfullname);
            image.Dispose();
            return newname;

        }

        //根据缩略图与原图的缩放比例来截取上传缩略图并获取的名称
        public string GetSmallImageByMultiple(string fullname,string suffix,string path)
        {
            double multiple = Config.multiple;
            System.Drawing.Image image, NewImage;
            image = System.Drawing.Image.FromFile(fullname);
            System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            int width = Convert.ToInt32(image.Width * multiple);
            int height = Convert.ToInt32(image.Height * multiple);
            NewImage = image.GetThumbnailImage(width, height, callb, new System.IntPtr());
            string newname = GetOnlyName(suffix);
            string newfullname = Path.Combine(path, GetOnlyName(suffix));
            NewImage.Save(newfullname);
            image.Dispose();
            return newname;
        }

        /// <summary>
        /// 提供一个回调方法，用于确定 GetThumbnailImage 方法应在何时提前取消执行
        /// </summary>
        public static bool ThumbnailCallback()
        {
            return false;
        }

        //更新单个图片
        public override ReturnModel UpdateFile(System.Web.UI.WebControls.FileUpload fileupload, string filename)
        {
            string uploadPath = Config.ImageUploadPath.Trim() == "" ? Config.DefaultPath : Config.ImageUploadPath;      //获取上传路径
            string filepath = Config.ImageServerPath.Trim() == "" ? Config.DefaultServerPath : Config.ImageServerPath;
            return base.ModifyFile(fileupload, filename, uploadPath, filepath);
        }

        //更新有缩略图的图片
        public ReturnModel UpdateFile(System.Web.UI.WebControls.FileUpload fileupload, string filename,string filenameA, ZoomType zoomtype)
        {
            string uploadPath = Config.ImageUploadPath.Trim() == "" ? Config.DefaultPath : Config.ImageUploadPath;      //获取上传路径
            string filepath = Config.ImageServerPath.Trim() == "" ? Config.DefaultServerPath : Config.ImageServerPath;
            return ModifyFile(fileupload, filename, uploadPath, filepath, zoomtype,filenameA);
        }

        //真正修改图片和缩略图
        public ReturnModel ModifyFile(System.Web.UI.WebControls.FileUpload fileupload, string filename, string uploadpath, string modifypath,ZoomType zoomtype,string filenameA)
        {
            ReturnModel model = new ReturnModel();
            if (fileupload.HasFile)
            {
                try
                {
                    File.Delete(Path.Combine(modifypath, filename));
                    File.Delete(Path.Combine(modifypath, filenameA));
                    model = SaveFile(fileupload, zoomtype);
                    if (!new FileDal().Delete(filename))
                        model.IsSuccess = false;
                }
                catch (Exception)
                {
                  
                    model.IsSuccess = false;
                }
            }
            else
            {
                try
                {
                    File.Delete(Path.Combine(modifypath, filename));
                    model.IsSuccess = true;
                }
                catch (Exception)
                {
                    model.IsSuccess = false;
                }
            }
            return model;
        }
    }
}
